<template>
  <el-dialog
    title="上传文件"
    :close-on-click-modal="false"
    :visible.sync="visible"
    v-loading="isLoading"
    v-if="visible"
  >
    <el-upload
      drag
      :action="url"
      accept=".docx"
      :before-upload="beforeUploadHandle"
      :on-success="successHandle"
      style="text-align: center"
    >
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      <div class="el-upload__tip" slot="tip">只支持docx格式的文档!</div>
    </el-upload>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      isLoading: false,
      visible: false,
      url: "",
      responseData: {},
    };
  },
  methods: {
    init(path) {
      this.url = this.$http.adornUrl(
        `${path}?token=${this.$cookie.get("token")}`
      );
      this.visible = true;
    },
    // 上传之前
    beforeUploadHandle(file) {
      // if (
      //   file.type !==
      //   "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
      // ) {
      //   this.$message.error("只支持docx格式的文档!");
      //   return false;
      // }
      return true;
    },
    // 上传成功
    successHandle(response, file) {
      this.isLoading = true;
      if (response && response.code === 0) {
        this.responseData = response.examData;
        this.$message.success(response.msg);
        this.isLoading = false;
        this.$emit("refreshDataList", this.responseData);
      } else {
        this.isLoading = false;
        this.$alert(response.msg, "错误", {
          confirmButtonText: "确定",
          type: "error",
          callback: (action) => {
            this.$message.error("试卷提取失败!");
          },
        });
      }
      this.visible = false;
    },
    // // 弹窗关闭时
    // closeHandle() {
    //   this.$emit("refreshDataList", this.responseData);
    // },
  },
};
</script>

